package net.alteiar.images.metadata;

import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

import net.alteiar.Array2D;
import net.alteiar.transformation.TransformationFunction;

public class MultipleLayerImageInfo extends IImageInfo {
	private final List<ImageInfo> info;

	public MultipleLayerImageInfo(ImageInfo first) {
		info = new ArrayList<>();
		info.add(first);
	}

	public void addImageInfo(ImageInfo info) {
		this.info.add(info);
	}

	@Override
	public synchronized void addObserver(Observer o) {
		for (ImageInfo current : info) {
			current.addObserver(o);
		}
	}

	@Override
	public synchronized void deleteObserver(Observer o) {
		for (ImageInfo current : info) {
			current.deleteObserver(o);
		}
	}

	@Override
	public int getSquareSize() {
		return 1;
	}

	/**
	 * 
	 * @param x
	 *            - x position on orginal image
	 * @param y
	 *            - y position on original image
	 * @return the value of the block at the original image position
	 */
	@Override
	public int getValueAt(int x, int y) {
		int max = Integer.MIN_VALUE;
		for (ImageInfo current : info) {
			max = Math.max(current.getValueAt(x, y), max);
		}
		return max;
	}

	@Override
	public int getValue(int x, int y) {
		int max = Integer.MIN_VALUE;
		for (ImageInfo current : info) {
			max = Math.max(current.getValueAt(x, y), max);
		}
		return max;
	}

	@Override
	public int[] getValues(TransformationFunction fct) {
		Array2D vals = new Array2D(getImageWidth(), getImageHeight());
		for (int x = 0; x < getImageWidth(); ++x) {
			for (int y = 0; y < getImageHeight(); ++y) {
				vals.set(x, y, fct.transform(getValueAt(x, y)));
			}
		}
		return vals.getArray();
	}

	@Override
	public void getValues(int[] vals, TransformationFunction fct) {
		Array2D values = new Array2D(vals, getImageWidth());

		for (int x = 0; x < getImageWidth(); ++x) {
			for (int y = 0; y < getImageHeight(); ++y) {
				values.set(x, y, fct.transform(getValueAt(x, y)));
			}
		}
	}

	@Override
	public int getImageWidth() {
		IImageInfo basic = info.get(0);
		return basic.getImageWidth();
	}

	@Override
	public int getImageHeight() {
		IImageInfo basic = info.get(0);
		return basic.getImageHeight();
	}

	@Override
	public int getWidthSquare() {
		return getImageWidth();
	}

	@Override
	public int getHeightSquare() {
		return getImageHeight();
	}
}
